package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Category;
import com.itheima.domain.Movies;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@CrossOrigin(value = "{http://127.0.0.1:81,http://127.0.0.1:82}")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //分页查询
    @GetMapping("/findByPage")
    public PageInfo<Category> findByPage(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize,String keyword){
        //调用方法
        return categoryService.findByPage(pageNum,pageSize,keyword);
    }
    //添加
    @PostMapping("/save")
    public String save(@RequestBody Category category){
        //调用方法
        categoryService.save(category);
        return "ok";
    }
    //查找
    @GetMapping("/findById")
    public Category findById(Integer id){

        return categoryService.findById(id);
    }
    //删除
    @DeleteMapping("/deleteById")
    public String deleteById(Integer id){
        categoryService.deleteById(id);
        return "ok";
    }
    //批量删除
    @DeleteMapping("/deleteIds")
    public String deleteIds(Integer[] ids){
        categoryService.deleteIds(ids);
        return "ok";
    }
    //查询所有分类名及该类名下的所有电影
    @GetMapping("/findAllCategoryAndMovies")
    public Map<String,List<Movies>> findAllCategoryAndMovies(){
       return categoryService.findAllCategoryAndMovies();
    }
    //查询所有分类
    @GetMapping("/findAll")
    public List<Category> findAll(){
        return categoryService.findAll();
    }
}
